package com.sakura.hk.sakura.springboot.plugin.task;

import com.sakura.hk.sakura.springboot.plugin.SakuraBootType;
import com.sakura.hk.sakura.springboot.plugin.SakuraExtension;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.internal.AbstractTask;
import org.gradle.api.tasks.TaskAction;

import java.io.File;

/**
 * sakura 插件化编译
 * @Author: 贺坤
 * @Date: 2021/7/13 16:26
 */
public class SakuraPlugInBuild extends DefaultTask {
    /**
     * 当前任务名称
     */
    public static final String MY_TASK_NAME = "sakuraPlugInBuild";
    /**
     * 构建目录
     */
    private File buildDir;
    /**
     * 构建jar名称
     */
    private String buildJarName;
    /**
     * 后缀
     */
    private String suffix;

    /**
     * 打包类型
     */
    private SakuraBootType type ;

    @TaskAction
    public void action() {
        init();
        System.out.println("打包完成,谢谢您的使用!");
        System.out.print("构建的jar包位于: ");
        System.out.print(buildDir.getAbsolutePath());
        System.out.print(File.separator);
        System.out.print(buildJarName);
        System.out.print(suffix != null ? ("-" + suffix) : "");
        System.out.print(".");
        System.out.println(type.getValue());
        System.out.println("====================");
    }

    private void init() {
        SakuraExtension sakuraExtension = getProject().getExtensions().getByType(SakuraExtension.class);
        this.buildJarName = sakuraExtension.getBuildJarName();
        this.suffix = sakuraExtension.getSuffix();
        this.buildDir = sakuraExtension.getBuildDir();
        this.type = sakuraExtension.getType();
    }

    public static AbstractTask foundMyTask(Project project) {
        SakuraPlugInBuild task = project.getTasks().create(MY_TASK_NAME, SakuraPlugInBuild.class);
        task.dependsOn(ClearCache.MY_TASK_NAME);
        task.setDescription("sakura插件打包器");
        return task;
    }

}
